﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SimpleFileDataAndViewModels;
using ViewModelType = SimpleFileDataAndViewModels.ImageProviderCollection;

namespace SimpleImageApplication
{
    /// <summary>
    /// Interaction logic for ThumbnailCollectionView.xaml
    /// </summary>
    public partial class ThumbnailCollectionView : UserControl
    {
        public VirtualizingTilePanel _wrapPanel;

        public static readonly DependencyProperty ViewModelProperty =
            DependencyProperty.Register("ViewModel", typeof(ViewModelType), typeof(ThumbnailCollectionView), new PropertyMetadata(null, OnViewModelPropertyChanged));

        public ViewModelType ViewModel
        {
            get { return (ViewModelType)GetValue(ViewModelProperty); }
            set { SetValue(ViewModelProperty, value); }
        }

        private static void OnViewModelPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ThumbnailCollectionView source = d as ThumbnailCollectionView;
            source.UpdateViewModel(e.NewValue as ViewModelType, e.OldValue as ViewModelType);
        }

        private void UpdateViewModel(ViewModelType viewModel, ViewModelType viewModelOld)
        {
            this.DataContext = viewModel;
            lstItems.ItemsSource = viewModel;
        }

        public ThumbnailCollectionView()
        {
            InitializeComponent();
        }

        private void itemsPanel_Loaded(object sender, RoutedEventArgs e)
        {
            _wrapPanel = sender as VirtualizingTilePanel;
            _wrapPanel.CollectionParent = this;            
            _wrapPanel.SetValue(VirtualizingTilePanel.ChildSizeXProperty, 100D);
            _wrapPanel.SetValue(VirtualizingTilePanel.ChildSizeYProperty, 80D);        
        }
    }
}
